<template>
  <el-dialog
    :model-value="dialogFormVisible"
    title="分配角色"
    @close="quXiaoFn"
  >
    <el-form :model="form" :rules="rules" ref="formRef">
      <el-form-item label="账号:" prop="username">
        <el-input v-model="form.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名:" prop="nickName">
        <el-input v-model="form.nickName" autocomplete="off" />
      </el-form-item>
      <el-form-item label="邮箱:" prop="email">
        <el-input v-model="form.email" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码:" prop="password">
        <el-input
          v-model="form.password"
          type="password"
          autocomplete="off"
          :disabled="userId !== 0"
        />
      </el-form-item>
      <el-form-item label="备注:">
        <el-input
          v-model="form.note"
          :rows="3"
          type="textarea"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item label="是否启用:">
        <el-radio-group v-model="form.status" class="ml-4">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="quXiaoFn">取消</el-button>
        <el-button type="primary" @click="addUserFn">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script lang="ts" setup>
import { ElMessage, ElMessageBox } from "element-plus";
import { ref, reactive, watch } from "vue";
import { getUserByIdApi, updateUserApi, userRegisterApi } from "../api";
import type { FormInstance } from "element-plus";
const emit = defineEmits(["hiddenForm"]);
// 表单验证的ref值
const formRef = ref<FormInstance>();

//表单 邮箱验证
const validateEmail = (rule: any, value: any, callback: any) => {
  if (!/^\w+[@]\w+[.]\w+/.test(value)) {
    return callback(new Error("邮箱格式不正确"));
  } else {
    callback();
  }
};
// 表单验证
const rules = reactive({
  username: [{ required: true, message: "请输入账号名", trigger: "blur" }],
  nickName: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  email: [{ validator: validateEmail, trigger: "blur", required: true }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});
const addUser = () => {
  ElMessageBox.confirm("此操作将添加当前用户信息，是否继续？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true,
  })
    .then(() => {
      userRegisterApi(form).then((res) => {
        if (res.code === 200) {
          // 提示
          ElMessage.success("添加成功");
          // 把dialog关闭
          emit("hiddenForm", true);
          // 刷新数据
          form.note = "";
          // 清空表单
          formRef.value?.resetFields();
        }
      });
    })
    .catch(() => {});
};

const editUser = () => {
  ElMessageBox.confirm("您确定要修改当前用户信息吗？", "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
    draggable: true,
  })
    .then(() => {
      updateUserApi(form).then((res) => {
        if (res.code === 200) {
          ElMessage.success("修改成功");
          emit("hiddenForm", true);
          // 清空表单
          formRef.value?.resetFields();
          form.note = "";
        }
      });
    })
    .catch(() => {
      emit("hiddenForm", true);
      formRef.value?.resetFields();
      form.note = "";
    });
};
// 点击添加数据
const addUserFn = () => {
  formRef.value?.validate((ok: boolean) => {
    if (ok) {
      if (props.userId === 0) {
        addUser();
      } else {
        editUser();
      }
    }
  });
};
const quXiaoFn = () => {
  emit("hiddenForm");
  formRef.value?.resetFields();
  form.note = "";
};
// 父传子接收参数
const props = defineProps({
  dialogFormVisible: {
    type: Boolean,
    default: false,
  },
  userId: {
    type: Number,
    default: 0,
  },
});
// 点击编辑传入id进行监听值的变化获取对应的值请求Api
watch(
  () => props.userId,
  () => {
    if (props.userId !== 0) {
      getUserByIdApi(props.userId).then((res) => {
        if (res.code === 200) {
          // console.log(res);
          Object.assign(form, res.data);
        }
      });
    }
  }
);
//添加的数据项
const form = reactive<IList>({
  email: "",
  nickName: "",
  note: "",
  password: "",
  username: "",
  status: 0,
});
</script>

<style lang="scss" scoped></style>
